package com.haixiaoke.saas.enterprise.repairs.controller;

import java.util.List;

import cn.hutool.core.util.StrUtil;
import com.haixiaoke.saas.common.annotation.Log;
import com.haixiaoke.saas.common.constant.PlatformConstants;
import com.haixiaoke.saas.common.core.domain.AjaxResult;
import com.haixiaoke.saas.common.core.page.TableDataInfo;
import com.haixiaoke.saas.common.enums.BusinessType;
import com.haixiaoke.saas.repairs.domain.Repairs;
import com.haixiaoke.saas.repairs.service.IRepairsService;
import com.haixiaoke.saas.system.BaseController;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 报修Controller
 * 
 * @author panda
 * @date 2025-07-05
 */
@RestController
@RequestMapping("/enterprise/repairs/repairs")
public class EnterpriseRepairsController extends BaseController {
    @Autowired
    private IRepairsService repairsService;

    /**
     * 查询报修列表
     */
    @PreAuthorize("@ss.hasPermi('enterprise:repairs:page')")
    @GetMapping("/page")
    public TableDataInfo list(Repairs repairs) {
        startPage();
        repairs.setTenantType(getLoginUser().getTenantId());
        if (StrUtil.equals(PlatformConstants.PC_BUSINESS, getLoginUser().getPlatform())) {
            repairs.setTenantIds(getTenantIds());
        }
        if (StrUtil.equals(PlatformConstants.MP_WECHAT_BUSINESS, getLoginUser().getPlatform())) {
            repairs.setTenantId(getLoginUser().getTenantId());
        }
        List<Repairs> list = repairsService.selectRepairsList(repairs);
        return getDataTable(list);
    }

    /**
     * 获取报修详细信息
     */
    @PreAuthorize("@ss.hasPermi('enterprise:repairs:query')")
    @GetMapping(value = "/{repairsId}")
    public AjaxResult getInfo(@PathVariable("repairsId") Long repairsId) {
        return success(repairsService.selectRepairsByRepairsId(repairsId));
    }

    /**
     * 完成报修
     */
    @PreAuthorize("@ss.hasPermi('enterprise:repairs:edit')")
    @Log(title = "报修", businessType = BusinessType.UPDATE)
    @PutMapping("/complete/{repairsId}")
    public AjaxResult edit(@PathVariable Long repairsId) {
        return repairsService.completeRepairs(repairsId, getUsername(), getLoginUser().getTenantId());
    }

    /**
     * 删除报修
     */
    @PreAuthorize("@ss.hasPermi('enterprise:repairs:remove')")
    @Log(title = "报修", businessType = BusinessType.DELETE)
	@DeleteMapping("/{repairsId}")
    public AjaxResult remove(@PathVariable Long repairsId) {
        return toAjax(repairsService.softDeleteRepairsByRepairsId(repairsId, getLoginUser().getTenantId()));
    }
}
